package com.song.shop.service.impl;

import com.song.shop.dao.ShopDao;
import com.song.shop.dynamic.DS;
import com.song.shop.entity.Shop;
import com.song.shop.service.ShopService;
import com.song.shop.service.TestService;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
@Service
public class ShopServiceImpl implements ShopService {
	
	@Autowired
	private ShopDao shopDao ;
	@Autowired
    private TestService testService ;

	
	@Override
	public Map<String, Object> testData() {
		Map<String, Object> testData = shopDao.testData();
		return testData;
	}

	@Override
	public Shop getOne() {
		
		Shop shop = shopDao.getOne();
		return shop;
	}
	/**
	 *  获取所有产品数据信息
	 */
	@Override
	public List<Shop> getAll() {
		List<Shop> list = shopDao.getAll();
        list.forEach(System.out::println);
		return list;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public String insertShop(Shop shop) throws Exception {
        System.out.println(shop.getId());
        shopDao.insertShop(shop);
        Shop shop1 = new Shop();
        testService.insertRollBackTest(shop);
        return "操作成功";
	}

    public void insertShopRollBack() {
        System.out.println("---------------------");
        Shop shop1 = new Shop();
        shopDao.insertShop(shop1);
    }

    @DS(value = "datasource2")
	@Override
	public String insertShop2(Shop shop) {
		int result = shopDao.insertShop(shop);
		if (result != 1){
			return "操作失败";
		}

		return "操作成功";
	}

    @Override
    public int deleteById(String id) {

	    return shopDao.delateById(id);
    }

    /**
	 * 更新Shop
	 * @param shop
	 * @return
	 */
	@Override
	public String updateShop(Shop shop) throws Exception {

		validate(shop);
		return null;
	}



	private void validate(Shop shop) throws Exception {

		if ( StringUtils.isBlank(shop.getId())){
			throw new Exception( "id异常" );
		}
	}

	private int insertShop22() throws Exception {
	    Shop shop = new Shop();
	    shop.setId("1111111111");

        int i = shopDao.insertShop(shop);
        /*if (i == 1){
            throw new Exception("zidingyi changc");
        }*/
        return i ;
    }

    public List<Shop> queryByIds(List<String> ids){
	    return shopDao.queryByIds(ids);
    }

}
